﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NullSuicide
{
    public partial class Mainform : Form
    {
        public delegate void addplayerDelegate(string name, int possition, bool inRaid);
        private Players _players;
        //private Items _items;
        
        //Dictionary<int, Item> _items;
        public Mainform()
        {
            InitializeComponent();
            //_items = new Items();
            _players = DataConnector.LoadPlayers();
            // _items = DataConnector.LoadItems();

            RefreshDataset();
        }

        private void RefreshDataset()
        {
            _players.sortlist();
            dsPlayers.Tables["Players"].Clear();

            foreach (Player player in _players.ListOfPlayers)
            {
                dsPlayers.Tables["Players"].Rows.Add(player.ID, player.Name, player.InRaid, player.Position);
            }
        }

        private void AddPlayer_Click(object sender, EventArgs e)
        {
            AddPlayer addplayer = new AddPlayer(new addplayerDelegate(AddPlayer), this);
            addplayer.ShowDialog();
        }

        public void AddPlayer(string name, int possition, bool inRaid)
        {
            if (possition > 0)
            {
                _players.AddPlayer(name, possition, inRaid);
            }
            else
            {
                _players.AddPlayer(name, inRaid);
            }
            RefreshDataset();
        }

        private void bSuicide_Click(object sender, EventArgs e)
        {
            
            if (gPlayers.SelectedRows.Count == 1)
            {
                int playerID = Convert.ToInt32(gPlayers.SelectedRows[0].Cells[0].Value);
                string playername = gPlayers.SelectedRows[0].Cells[2].Value.ToString();

                DialogResult result = MessageBox.Show("Suicide player: " + playername + "?", "Suicide" , MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    _players.Suicide(playerID, false);
                }
            }

            RefreshDataset();
        }

        private void gPlayers_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (gPlayers.SelectedRows.Count == 1)
            {
                int playerID = Convert.ToInt32(gPlayers.SelectedRows[0].Cells[0].Value);
                bool inRaid = Convert.ToBoolean(gPlayers.SelectedRows[0].Cells[3].Value);
                _players.chanceInRaid(playerID, inRaid);
            }
            
        }

        private void bDeletePlayer_Click(object sender, EventArgs e)
        {
            if (gPlayers.SelectedRows.Count == 1)
            {
                string playername = gPlayers.SelectedRows[0].Cells[2].Value.ToString();
                int playerID = Convert.ToInt32(gPlayers.SelectedRows[0].Cells[0].Value);

                DialogResult result = MessageBox.Show("Delete player: " + playername + "?", "Delete player", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    _players.DeletePlayer(playerID);
                    RefreshDataset();
                }
            }
        }

        private void gPlayers_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (gPlayers.SelectedRows.Count == 1)
            {
                int playerID = Convert.ToInt32(gPlayers.SelectedRows[0].Cells[0].Value);
                Player player = _players.getPlayer(playerID);
                dsItems.Tables["Items"].Clear();

                foreach (LootItem lootitem in player.Items)
                {
                    dsItems.Tables["Items"].Rows.Add(lootitem.ItemID, "", lootitem.IsOffspec);
                }
            }
            else
            {
                // empty DS
                dsItems.Tables["Items"].Clear();
            }
        }

        private void gItems_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (gItems.SelectedRows.Count == 1)
            {
                int itemID = Convert.ToInt32(gItems.SelectedRows[0].Cells[0].Value);
                HtmlDocument doc = (HtmlDocument)wbItem.Document;

                doc.OpenNew(false);
                //doc.Write(_items[itemID].HTML_BODY);
            }
        }
    }
}
